package com.example.adopt.controller;


import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.adopt.entity.Pet;
import com.example.adopt.entity.Pettype;
import com.example.adopt.entity.Product;
import com.example.adopt.entity.Producttype;
import com.example.adopt.service.PettypeService;
import com.example.adopt.service.ProductService;
import com.example.adopt.service.ProducttypeService;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import lombok.AllArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;
import java.util.UUID;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author wjl
 * @since 2024-09-13
 */
@RestController
@RequestMapping("/product")
public class ProductController {
    @Resource
    ObjectMapper objectMapper;
    @Autowired
    ProductService productService;
    @Autowired
    ProducttypeService producttypeService;
    @RequestMapping("/product")
    public String selectAll(String pname,
                                Integer page,Integer size) throws JsonProcessingException {
        QueryWrapper<Product> wrapper = new QueryWrapper<>();
        if(!StrUtil.isBlank(pname)){
            wrapper.like("pname", pname);
        }
        Page<Product> page1 = new Page<>(page, size);
        IPage<Product> petIPage = productService.page(page1,wrapper);
        String s = objectMapper.writeValueAsString(petIPage);
        return s;
    }
    @RequestMapping("/type")
    public String getType() throws JsonProcessingException {
        List<Producttype> type = producttypeService.list();
        String s = objectMapper.writeValueAsString(type);
        return s;
    }
    @PostMapping("/add")
    public String add(@RequestBody Product product){
        QueryWrapper<Product> wrapper = new QueryWrapper<>();
        QueryWrapper<Product> wrapper1 = wrapper.eq("pname",product.getPname())
                .eq("price",product.getPrice());
        Product one = productService.getOne(wrapper1);
        if(one != null){
            return "添加重复";
        }
        boolean save = productService.save(product);
        String result = save == true ? "success" : "false";
        return result;
    }
    @DeleteMapping("/del")
    public String del(String pid){
        boolean remove = productService.removeById(pid);
        String result = remove == true ? "success" : "false";
        return result;
    }
    @PutMapping("/update")
    public String update(@RequestBody Product product){
        boolean save = productService.updateById(product);
        String result = save == true ? "success" : "添加失败";
        return result;
    }
    @PostMapping("/addtype")
    public String addtype(@RequestBody Producttype producttype){
        QueryWrapper<Producttype> queryWrapper = new QueryWrapper<>();
        QueryWrapper<Producttype> queryWrapper1 = queryWrapper.eq("name", producttype.getName());
        Producttype one = producttypeService.getOne(queryWrapper1);
        if(one != null ){
            return "该类型已存在，无需添加";
        }
        boolean save = producttypeService.save(producttype);
        String result = save == true ? "success" : "false";
        return result;
    }

}

